package com.yskj.otms.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class JsonUtil {


    public static JSONObject getJSONObject(HttpServletRequest request) throws Exception{

        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }
        // 将资料解码
        return JSONObject.fromObject(sb.toString());
    }

    public static String getJSONObjectString(JSONObject obj, String filed) {
        String str;
        try {
            str = obj.getString(filed).trim();
        }catch (JSONException e) {
            str = "";
        }

        return str;
    }

    public static String getJSONObjectString2(JSONObject obj, String filed) {
        if (null == obj) {
            return null;
        }
        String str;
        try {
            str = obj.getString(filed).trim();
        }catch (JSONException e) {
            str = null;
        }

        return str;
    }

    public static JSONArray getJSONArray(JSONObject obj, String filed) {
        JSONArray jsonArray = null;
        try {
            jsonArray = obj.getJSONArray(filed);
        }catch (JSONException e) {
            jsonArray = null;
        }

        return jsonArray;
    }

}
